<?php
$cs = Yii::app()->clientScript;
$cs->registerCoreScript('bbq');
$baseScriptUrl=Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('zii.widgets.assets')).'/gridview';
$cs->registerCssFile($baseScriptUrl.'/styles.css');

$script = "
jQuery('body').delegate('.grid-view .button-column .update, .grid-view .create', 'click', function(){
    var url = jQuery(this).attr('href');
    jQuery('.edit-form').load(url);
    return false;
});
jQuery('.grid-view .create').trigger('click');
";
$cs->registerScript(__CLASS__.'#UpdateRole', $script);

$script = "
jQuery('body').delegate('.edit-form .buttons input', 'click', function(){
    var form = jQuery(this).closest('form');
    var url = form.attr('action');
    var data = form.serialize();
    data = jQuery.deparam.querystring(data);
    jQuery('.edit-form').load(url, data, function(){
        jQuery('.grid-view').load('".$this->createUrl('/Admin/permission/role')." .grid-view > *');
    });
    return false;
});
";
$cs->registerScript(__CLASS__.'#SaveRole', $script);

$script = "
jQuery('body').delegate('.grid-view .button-column .delete', 'click', function(){
    if (confirm('Are you sure want to delete this role?'))
    {
        var url = jQuery(this).attr('href');
        jQuery.get(url, function(){
            jQuery('.grid-view').load('".$this->createUrl('/Admin/permission/role')." .grid-view > *');
            jQuery('.edit-form').html('');
        });
    }
    return false;
});
";
$cs->registerScript(__CLASS__.'#DeleteRole', $script);
?>
<style type="text/css">
.grid-view {width: 60%; float: left;}
.edit-form {width: 38%; float: right;}
.edit-form textarea,
.edit-form input {
    width: 250px;
    border:1px solid #96A6C5;
    padding: 3px;
}
.edit-form textarea {
    height: 70px;
    width: 100%;
}
.edit-form .checkbox input,
.edit-form .buttons input {width: auto;}
.edit-form .checkbox label {display: inline;}
</style>
<div class="grid-view">
<table class="items">
    <tr>
        <th>Role</th>
        <th>Description</th>
        <th>BizRule</th>
        <th>Data</th>
        <th>
            <?php echo CHtml::link(CHtml::image(Yii::app()->theme->baseUrl.'/images/addicon.gif', 'Create New Role'), array('updateRole'), array('title'=>'Create New Role', 'class'=>'create'));?>
        </th>
    </tr>
    <?php if (is_array($roles) && count($roles)):?>
    <?php foreach ($roles as $index => $role):?>
    <tr class="<?php echo $index%2 ? 'odd' : 'even';?>">
        <td><?php echo $role->name;?></td>
        <td><?php echo CHtml::encode($role->description);?></td>
        <td><?php echo CHtml::encode($role->bizRule);?></td>
        <td><?php echo CHtml::encode($role->data);?></td>
        <td class="button-column">
            <?php echo CHtml::link(CHtml::image($baseScriptUrl.'/update.png', 'Update'), array('updateRole', 'role'=>$role->name), array('title'=>'Update', 'class'=>'update'));?>
            <?php echo CHtml::link(CHtml::image($baseScriptUrl.'/delete.png', 'Delete'), array('/Core/service/ajax', 'SID'=>'Admin.permission.deleteAuthItem', 'name'=>$role->name), array('title'=>'Delete', 'class'=>'delete'));?>
        </td>
    </tr>
    <?php endforeach;?>
    <?php endif;?>
</table>
</div>
<div class="edit-form">
    
</div>
<div style="clear: both;"></div>